/*
 *  Copyright 2016 http://www.kedacomframework.org
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *
 */

package com.kedacom.ctsp.authority.starter;

import com.kedacom.ctsp.authority.controller.*;
import com.kedacom.ctsp.authority.service.UserService;
import com.kedacom.ctsp.authority.service.simple.DefaultAuthenticationService;
import com.kedacom.ctsp.authority.service.validator.PasswordStrengthValidator;
import com.kedacom.ctsp.authority.service.validator.UsernameValidator;
import com.kedacom.ctsp.authz.AuthenticationService;
import com.kedacom.ctsp.starter.springboot.controller.FileUploadController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;


/**
 * @author sunchenjie
 */
@Configuration
@ComponentScan
public class AuthorityAutoConfiguration {


    /**
     * 密码强度验证
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(PasswordStrengthValidator.class)
    public PasswordStrengthValidator passwordStrengthValidator() {
        return new PasswordStrengthValidator() {
            @Override
            public boolean validate(String data) {
                return StringUtils.isNotBlank(data) && data.length() >= 5;
            }

            @Override
            public String getErrorMessage() {
                return "密码强度太弱";
            }
        };
    }

    /**
     * 用户名长度验证
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(UsernameValidator.class)
    public UsernameValidator usernameValidator() {

        return new UsernameValidator() {
            @Override
            public boolean validate(String data) {
                return StringUtils.isNotBlank(data) && data.length() >= 5;
            }

            @Override
            public String getErrorMessage() {
                return "用户名长度需5位及以上";
            }
        };
    }

    @Bean
    @ConditionalOnMissingBean(AuthenticationService.class)
    @ConditionalOnBean(UserService.class)
    public AuthenticationService authenticationService() {
        return new DefaultAuthenticationService();
    }

    /**
     * @return 用户管理controller
     */
    @Bean
    @ConditionalOnMissingBean(AuthorityUserController.class)
    public AuthorityUserController userController() {
        return new AuthorityUserController();
    }


    /**
     * @return 角色管理controller
     */
    @Bean
    @ConditionalOnMissingBean(AuthorityRoleController.class)
    public AuthorityRoleController roleController() {
        return new AuthorityRoleController();
    }


    /**
     * @return 资源-配置管理controller
     */
    @Bean
    @ConditionalOnMissingBean(AuthorityResourceController.class)
    public AuthorityResourceController resourceController() {
        return new AuthorityResourceController();
    }

    /**
     * @return 资源管理controller
     */
    @Bean
    @ConditionalOnMissingBean(AuthorityResourceSettingController.class)
    public AuthorityResourceSettingController resourceSettingController() {
        return new AuthorityResourceSettingController();
    }

    /**
     * @return 部门管理controller
     */
    @Bean
    @ConditionalOnMissingBean(AuthorityDepartmentController.class)
    public AuthorityDepartmentController departmentController() {
        return new AuthorityDepartmentController();
    }

    /**
     * @return 人员管理controller
     */
    @Bean
    @ConditionalOnMissingBean(AuthorityPersonController.class)
    public AuthorityPersonController personController() {
        return new AuthorityPersonController();
    }

    /**
     * 提供authoroty基本功能
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(AuthorityController.class)
    public AuthorityController authorityController() {
        return new AuthorityController();
    }

    /**
     * @return 分管管理controller
     */
    @Bean
    @ConditionalOnMissingBean(AuthorityChargeRelationController.class)
    public AuthorityChargeRelationController chargeRelationController() {
        return new AuthorityChargeRelationController();
    }

    /**
     * @return 文件下载controller
     */
    @Bean
    @ConditionalOnMissingBean(FileUploadController.class)
    public AuthorityFileController fileController() {
        return new AuthorityFileController();
    }

    /**
     * 字典分类表
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(DataDictCategoryController.class)
    public DataDictCategoryController dataDictCategoryController() {
        return new DataDictCategoryController();
    }

    /**
     * 字典表
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(DataDictController.class)
    public DataDictController dataDictController() {
        return new DataDictController();
    }

    /**
     * @return 区域管理controller
     */
    @Bean
    @ConditionalOnMissingBean(AuthorityAreaController.class)
    public AuthorityAreaController areaController() {
        return new AuthorityAreaController();
    }

}
